package org.dcm4che3.tool.printscp;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.UID;
import org.dcm4che3.data.VR;
import org.dcm4che3.net.Association;
import org.dcm4che3.net.Commands;
import org.dcm4che3.net.Dimse;
import org.dcm4che3.net.Status;
import org.dcm4che3.net.pdu.PresentationContext;
import org.dcm4che3.net.service.AbstractDicomService;
import org.dcm4che3.net.service.DicomServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FilmSessionService extends AbstractDicomService {
	
	private static final Logger log = LoggerFactory.getLogger(FilmSessionService.class);
	
	public FilmSessionService(String ... sopClass) {
		super(sopClass);
	}
	
	@Override
	protected void onDimseRQ(Association as, PresentationContext pc, Dimse dimse, Attributes cmd, Attributes data)
			throws IOException {
		switch (dimse) {
        case N_CREATE_RQ:
            onNCreateRQ(as, pc, cmd, data);
            break;
        case N_SET_RQ:
            onNSetRQ(as, pc, cmd, data);
            break;
        case N_DELETE_RQ:
        	onNDeleteRQ(as, pc, cmd, data);
            break;
        case N_ACTION_RQ:
        	onNActionRQ(as, pc, cmd, data);
            break;
        default:
        	log.error("not regnoize command={}", dimse);
            throw new DicomServiceException(Status.UnrecognizedOperation);
        }
	}
	
	protected void onNActionRQ(Association as, PresentationContext pc,
            Attributes rq, Attributes rqAttrs) throws IOException {
		Attributes rsp = Commands.mkNActionRSP(rq, Status.Success);
		as.tryWriteDimseRSP(pc, rsp, null);
	}
	
	 protected void onNCreateRQ(Association as, PresentationContext pc,
	            Attributes rq, Attributes rqAttrs) throws IOException {
        Attributes rsp = Commands.mkNCreateRSP(rq, Status.Success);
        Attributes rspAttrs = create(as, rq, rqAttrs, rsp);
		 log.info("session rq==={}", rq);
		 log.info("rqAttrs rq==={}", rqAttrs);
		 
        as.tryWriteDimseRSP(pc, rsp, rspAttrs);
    }
	 
	 protected void onNDeleteRQ(Association as, PresentationContext pc,
	            Attributes rq, Attributes rqAttrs) throws IOException {
		 Attributes rsp = Commands.mkNDeleteRSP(rq, Status.Success);

		 Attributes rspAttrs = create(as, rq, rqAttrs, rsp);
		 as.tryWriteDimseRSP(pc, rsp, rspAttrs);
	 }

    protected Attributes create(Association as, Attributes rq,
            Attributes rqAttrs, Attributes rsp) throws DicomServiceException {
    	return null;
    }

    protected void onNSetRQ(Association as, PresentationContext pc,
            Attributes rq, Attributes rqAttrs) throws IOException {
        Attributes rsp = Commands.mkNSetRSP(rq, Status.Success);
        Attributes rspAttrs = set(as, rq, rqAttrs, rsp);
        as.tryWriteDimseRSP(pc, rsp, rspAttrs);
    }

    protected Attributes set(Association as, Attributes rq, Attributes rqAttrs,
            Attributes rsp) throws DicomServiceException {
    	log.info("set data=================== film session");
        return null;
    }
	
    
    private String getRandomInstanceSopUid() {
		Random r = new Random();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String randStr = String.valueOf(r.nextDouble()).substring(2,8);
		String uid = "1.2.689." + sdf.format(new Date()) + "." + randStr;
		return uid;
	}
}
